package com.shows.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shows.auto.JSONAuthentication;
import com.shows.entity.ShOrder;
import com.shows.entity.ShPrice;
import com.shows.mapper.ShPriceMapper;
import com.shows.mapper.ShUserMapper;
import com.shows.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/sh-price")
public class ShPriceController extends JSONAuthentication {
    @Autowired
    ShPriceMapper shPriceMapper;
    @Autowired
    ShUserMapper shUserMapper;
    @ApiOperation(value = "查看充值记录")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size,@RequestParam("current") int current) throws Exception{
        Page<ShPrice> page=new Page<>(current,size);
        Page<ShPrice> pages=shPriceMapper.selectPage(page,null);
        List<ShPrice> list = pages.getRecords();
        for(ShPrice a:list){
            a.setShUser(shUserMapper.selectById(a.getUserId()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("price",list);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看充值记录通过用户id")
    @PostMapping("/addByUserId")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size,@RequestParam("current") int current,@RequestParam("userId") int userId) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<ShPrice> page=new Page<>(current,size);
        Page<ShPrice> pages=shPriceMapper.selectPage(page,wrapper);
        List<ShPrice> list = pages.getRecords();
        int total=shPriceMapper.selectCount(null);
        for(ShPrice a:list){
            a.setShUser(shUserMapper.selectById(a.getUserId()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("price",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

